package cn.tinlan.shopcoin.app.web;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.base.BaseActivity;

/**
 * Created by Mortimer on 2018/11/21. 11:47
 * 佛祖🙏，永无bug。
 */
public class WebActivity extends BaseActivity {

    @BindView(R.id.web_title)
    TitleBar titleBar;
    @BindView(R.id.web_content)
    FrameLayout frameLayout;

    WebView mWebView;
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_web;
    }

    @Override
    public void initView() {
        super.initView();
        drawableDefaultBar();

        initTitleBar();
        initWebView();
    }

    private void initWebView() {
        mWebView = new WebView(getApplicationContext());
        frameLayout.addView(mWebView);

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {//页面加载完成
//                progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {//页面开始加载
//                progressBar.setVisibility(View.VISIBLE);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                Log.i("ansen","拦截url:"+url);
//                if(url.equals("http://www.google.com/")){
//                    Toast.makeText(MainActivity.this,"国内不能访问google,拦截该url",Toast.LENGTH_LONG).show();
//                    return true;//表示我已经处理过了
//                }
                return super.shouldOverrideUrlLoading(view, url);
            }

        });

        WebSettings webSettings= mWebView.getSettings();

// webview启用javascript支持 用于访问页面中的javascript
        webSettings.setJavaScriptEnabled(true);

        int type = getIntent().getIntExtra("type",0);

        switch (type) {
            case 0:
                break;
            case 1:
                String webUrl = getIntent().getStringExtra("webUrl");
                mWebView.loadUrl(webUrl);
                break;
            case 2:
                String webCode = getIntent().getStringExtra("webCode");
                mWebView.loadData(webCode,"text/html;charset=utf-8","utf-8");
                break;
        }



    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mWebView.canGoBack() && keyCode == KeyEvent.KEYCODE_BACK){//点击返回按钮的时候判断有没有上一页
            mWebView.goBack();// goBack()表示返回webView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initTitleBar() {
        String title = getIntent().getStringExtra("title");
        titleBar.setTitle(title.length() > 0 ? title : "详情");

        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mWebView.removeAllViews();
        ((ViewGroup) mWebView.getParent()).removeView(mWebView);
        mWebView.setTag(null);
        mWebView.clearHistory();
        mWebView.destroy();
        mWebView= null;


    }
}
